package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 留园网
 * @author KK
 *
 */
public class _6parkAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://news.6park.com/newspark/index.php\\?act=view&app=news&nid=[0-9]*",
		"http://site.6park.com/bolun/index.php\\?act=threadview&app=forum&tid=[0-9]*",
		"http://blog.6park.com/article.php\\?itemid=[0-9]*&type=blog",

	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("title").text();
			title = StringUtils.substringBeforeLast(title, "-");
			if(title!=null) title = title.replace("留园网-", "");
		}

		doc.select("#newscontent table ~ *").remove();
		doc.select("#newscontent table").remove();
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select("#newscontent"));
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select("pre"));
		if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select(".post-content"));


		date =DateUtils.matchDate(doc.select("div.articletime").text());
		if(date==null) date=DateUtils.matchDate(doc.select("span.date").text());
		if(date==null) date=DateUtils.matchDate(doc.select("span.story-tag").text());
		if(date==null) date=JSoupUtils.matchDate(doc, "来源:","送交者:","日期:");

		author = JSoupUtils.matchAuthor(doc, "送交者:","作者：");
		if(author!=null){
			author = author.replace("的博客主页", "");
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
